<?php
/**图片显示 组件
*使用示例:<?php $this->widget('ext.datepickerwidget',array('model'=>$model,'name'=>'vip_deadline','style'=>'border:1px solid #c5c5c5'));?>
*/
class ShowimgWidget extends CWidget
{
	public $model=null;
	public $name='date';
	public $logo='logo';

	public function init()
	{

	}

	public function run()
	{

		if(!empty($this->model))
		{
			$stri=$this->model[$this->logo];
		}
		else
		{
			$stri='';
		}
		$str = '<input name="'.$this->name.'" type="file" onchange="previewFile'.$this->name.'()" id="'.get_class($this->model).$this->name.'">';
		$img = '<img src="/img/'.$stri.'" height="200" alt="Image preview..." id="'.get_class($this->model).$this->name.'img">';
		$script = '<script>function previewFile'.$this->name.'() {
					  var preview = document.querySelector("#'.get_class($this->model).$this->name.'img");
					  var file    = document.querySelector("#'.get_class($this->model).$this->name.'").files[0];
					  var reader  = new FileReader();

					  reader.onloadend = function () {
					    preview.src = reader.result;
					  }

					  if (file) {
					    reader.readAsDataURL(file);
					  } else {
					    preview.src = "";
					  }
					}</script>';
		echo $str.$img.$script;
	}
}